package com.vae.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vae.goods.mapper.BrandMapper;
import com.vae.goods.service.BrandService;
import com.vae.mall.goods.api.entity.Brand;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品品牌表(Brand)服务实现类
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    /****
     * 多条件查询
     * @param brand
     * @return
     */
    @Override
    public List<Brand> queryList(Brand brand) {
        // 多条件构造器
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<Brand>();
        if(!StringUtils.isEmpty(brand.getName())){
            queryWrapper.like("name",brand.getName());
        }
        if(!StringUtils.isEmpty(brand.getName())){
            queryWrapper.eq("initial",brand.getInitial());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    /***
     * 分页查询
     * @param brand
     * @return
     */
    @Override
    public Page<Brand> queryPageList(Long currentPage, Long size, Brand brand) {
        // 封装查询条件
        Page<Brand> page = this.baseMapper.selectPage(
                new Page<Brand>(currentPage, size),
                new QueryWrapper<Brand>()
                        .like("name", brand.getName()));
        return page;
    }

    /**
     * 根据分类id查询品牌列表
     * @param id
     * @return
     */
    @Override
    public List<Brand> queryByCategoryId(Integer id) {

        //1. 根据分类id 查询对应的品牌id 集合
        List<Integer> brandIds = this.baseMapper.queryBrandIds(id);

        //2.根据品牌id集合去查询品牌列表
        List<Brand> brandList = this.baseMapper.selectBatchIds(brandIds);

        return brandList;
    }
}